package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractTaxRateDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractTaxRateService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Tag(name = "运营配置 - 税率配置")
@RestController
@RequestMapping(ApiConstants.NAME + "/contractTaxRate")
@RequiredArgsConstructor
public class ContractTaxRateController {

    @Resource
    private ContractTaxRateService contractTaxRateService;

    /**
     * 查询配置
     * @param apartmentId
     * @return
     */
    @GetMapping("/getContractTaxRate")
    public CommonResult<ContractTaxRateDO> getContractTaxRate(Long apartmentId) {
        return CommonResult.success(contractTaxRateService.getContractTaxRate(apartmentId));
    }

    /**
     * 修改税率配置
     * @param contractTaxRateDO
     * @return
     */
    @PostMapping("/updateContractTaxRate")
    public CommonResult<Boolean> updateContractTaxRate(@RequestBody ContractTaxRateDO contractTaxRateDO){
        return CommonResult.success(contractTaxRateService.updateById(contractTaxRateDO));
    }

    /**
     * 新增税率配置
     * @param contractTaxRateDO
     * @return
     */
    @PostMapping("/insertContractTaxRate")
    public CommonResult<Boolean>insertContractTaxRate(@RequestBody ContractTaxRateDO contractTaxRateDO){
        log.info("保存税率配置参数："+ JSONObject.toJSONString(contractTaxRateDO));
        ContractTaxRateDO contractTaxRate = contractTaxRateService.getContractTaxRate(contractTaxRateDO.getApartmentId());
        //校验
        if(contractTaxRate != null)
            return CommonResult.error(ErrorCodeConstants.EXIST_CONTRACT_TAX_TATE_ORDER);
        return CommonResult.success(contractTaxRateService.save(contractTaxRateDO));
    }
}
